<template>
  <div class="addClub">
    <div class="addClub-Form">
      <van-form @submit="onSubmit">
        <van-cell-group inset>
          <van-field
            v-model="clubName"
            name="clubName"
            label="名称"
            placeholder="社团的名称"
            :rules="[{ required: true, message: '请填写名称' }]"
          />
          <van-field
            v-model="clubDesc"
            type="textarea"
            name="clubDesc"
            label="描述"
            placeholder="社团描述"
            :rules="[{ required: true, message: '请填写描述' }]"
          />
          <van-field name="clubCover" label="封面">
            <template #input>
              <van-uploader 
                v-model="clubCover" 
                max-count="1"
              />
            </template>
          </van-field>
          <van-field
            v-model="clubTag"
            is-link
            readonly
            name="clubTag"
            label="标签"
            placeholder="点击选择标签"
            @click="showPicker = true"
            :rules="[{ required: true, message: '请填写标签' }]"
          />
        </van-cell-group>
        <div style="margin: 16px;">
          <van-button round block type="primary" native-type="submit">
            提交
          </van-button>
        </div>
      </van-form>
    </div>
    <div>
      <van-popup v-model:show="showPicker" position="bottom">
        <van-picker
          :columns="hobby"
          @confirm="onConfirm"
          @cancel="showPicker = false"
        />
      </van-popup>
    </div>
  </div>
</template>
<script>
import {Form,Field,CellGroup,Button,Uploader,Picker,Popup,Toast} from "vant"
import { getHobby, addClub, getClub } from "@/service/user"
export default {
  mounted(){
    getHobby().then(res=>{
      this.hobby = res.map(item=>item.hobbyName);
    })
  },
  components:{
    [Button.name]:Button,
    [Form.name]:Form,
    [Field.name]:Field,
    [CellGroup.name]:CellGroup,
    [Uploader.name]:Uploader,
    [Picker.name]:Picker,
    [Popup.name]:Popup
  },
  data(){
    return {
      showPicker:false,
      hobby:[],
      clubName:"",
      clubDesc:"",
      clubCover:[],
      clubTag:"",
    }
  },
  methods:{
    onConfirm(val){
      this.clubTag = val;
      this.showPicker = false
    },
    onSubmit(val){
      addClub({
        clubName:val.clubName,
        clubDesc:val.clubDesc,
        clubCover:val.clubCover[0],
        clubTag:val.clubTag
      }).then(res=>{
        res.status == 200 ? Toast.success(res.message) : Toast.success('发布失败');
        this.$emit('setAddShowClose')
      })
    }
  }
}
</script>
<style lang="less">
.addClub{
  height: calc(100vh - 50px);
  padding-top: 50px;
  background: #EFEFEF;
  position: relative;
  &::before{
    position: absolute;
    top:0;
    left: 0;
    content:"创建社团";
    width:100%;
    height: 50px;
    line-height:50px;
    background: #fff;
  }
  .addClub-Form{
    margin-top: 8px;
  }
}
</style>